﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Data.OleDb;
using System.Diagnostics;
using System.Configuration;

namespace 企业人事管理系统
{
   
    public partial class mainForm : Form
    {
        private DataListForm staffForm;
        private DataListForm departForm;
        private DataListForm jobForm;
        private DataListForm workTaskForm;
        private DataListForm contractForm;
        private UserManageForm userForm;

        private Form departTree;

        private Form father;

        //登录的用户信息
        private string userName;
       // private int userId;
        //private int userLevel;


        public mainForm(string name,Form f)
        {
            
           
            userName = name;
            father = f;

            this.Text = "企业人事管理系统" + "——当前用户：" + name;

            InitializeComponent();

            //初始化窗口
            staffForm = new DataListForm("员工信息管理",
                string.Format("select * from {0}", DataName.testTable));
            departTree = new DepartTree();
            departForm = new DataListForm("部门信息管理",
                string.Format("select * from {0}", DataName.departTable));
            jobForm = new DataListForm("职位信息管理",
                string.Format("select * from {0}", DataName.positionTable));
            workTaskForm = new DataListForm("工作任务管理",
                string.Format("select * from {0}", DataName.taskTable));
            contractForm = ContractForm.getDefaultForm();
            userForm = new UserManageForm(userName);

            //设置关闭不释放资源
            staffForm.addNotClose();
            departForm.addNotClose();
            jobForm.addNotClose();
            workTaskForm.addNotClose();
            contractForm.addNotClose();
            userForm.addNotClose();


            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string l = config.AppSettings.Settings["defaultBack"].Value;
            //MessageBox.Show(l);
            this.BackgroundImage = Image.FromFile(ImageChangeForm.initPath
                + "\\" + l);

        }

        private void 基本数据管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //basicForm.Show();
        }

        private void 部门管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           // wait w = new wait();
           // w.Show();
        }

        private void 部门关系管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            departTree.Show();
        }

        private void 部门信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            departForm.Show();
        }

        private void 员工信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        { 
            staffForm.Show();  
        }

        private void 职位信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            jobForm.Show();
        }

        private void 合同管理ToolStripMenuItem_Click(object sender, EventArgs e)
        { 
            contractForm.Show();
           /// System.Diagnostics.Process.Start("C:\\Users\\anhui\\Desktop\\员工.xlsx");
        }

        private void 工作任务管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            workTaskForm.Show();
        }

        private void 更改登录密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string name = userName;
           // int level = -1;
            string newPassWord = null;
            string[] pass = new string[3];

            //验证登录密码
            pass[0] = Interaction.InputBox("请重新验证登录密码", "密码验证","在此输入", -1, -1);
            try
            {
                OledbOP.initConnect(DataName.testConStr);
                OleDbDataReader re = OledbOP.getReader(
                    string.Format("select {1} from {0} where {2}='{4}' and {3}='{5}'",
                    DataName.userTable, //0
                    DataName.userLevel, //1
                    DataName.userName, //2
                    DataName.userPassword,//3
                    userName,//4
                    pass[0] //5
                    ));
                re.Read();
                if(re.HasRows)
                {
                    //level = re.GetInt32(0);
                    name = userName;
                }
                else
                {
                    name = null;
                }
                re.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
                name = null;
            }
            if(name==null)
            {
                MessageBox.Show("密码输入错误！");
                return;
            }

            //两次输入新密码
            pass[1] = Interaction.InputBox("请输入新密码", "密码修改", "在此输入", -1, -1);
            pass[2] = Interaction.InputBox("请再次输入新密码", "密码修改", "在此输入", -1, -1);
            if(!string.Equals(pass[1],pass[2]))
            {
                MessageBox.Show("两次输入的密码不同！");
                return;
            }

            //更新密码
            newPassWord = pass[1];
            int exe = -1;
            try
            {
                string sql = string.Format("update {2} set {4}='{0}' where {3}='{1}'",
                    newPassWord, //0
                    userName, //1
                    DataName.userTable, //2
                    DataName.userName, //3
                    DataName.userPassword //4
                    );
                exe = OledbOP.execute(sql);
            }catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
                exe = -1;
            }

            if(exe>0)
            {
                MessageBox.Show("密码修改成功！");
            }else
            {
                MessageBox.Show("密码修改未生效");
            }
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            father.Hide();
        }

        private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //重新启动程序
            Application.ExitThread();
            Application.Exit();
            Application.Restart();
            Process.GetCurrentProcess().Kill();
        }

        private void 管理系统用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userForm.showAfterTest();
        }

        private void 更换背景ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageChangeForm imageForm = new ImageChangeForm(this);
            imageForm.Show();
        }


        public void reLoadBack(EventArgs e)
        {
            OnLoad(e);
        }
    }
}
